$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'ProfessionInfo/Select',
        datatype: "json",
        colModel: [
            { label: '专业代码', name: 'professionCode', index:'profession_code',width: 40 },
             { label: '专业名称', name: 'professionName', index:'profession_name',width: 40,key: true },
             { label: '专业类型', name: 'professionType', index:'profession_type',width: 40 },
             { label: '学制', name: 'professionSchoolingYear',index:'profession_schooling_year', width: 40 },
             { label: '层次', name: 'professionLevel', index:'profession_level',width: 40 },
             { label: '所属学院代码', name: 'professionCollegeCode',index:'profession_college_code', width: 40 },
             { label: '国家统编专业代码', name: 'professionCountryCode',index:'profession_country_code', width: 40 },
             { label: '优先级', name: 'professionPriority',index:'profession_priority', width: 40 },
             { label: '国家统编专业名称', name: 'professionCountryName', index:'profession_country_name',width: 40 },
             { label: '学科类型', name: 'professionSubjectType',index:'profession_subject_type  ', width: 40 }
         ],
        viewrecords: true,
        height:'100%',
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        // rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
        ondblClickRow:function(rowid){
            var rowData = $("#jqGrid").jqGrid('getRowData',rowid);
            var rowName =$("#jqGrid").jqGrid('getGridParam','colNames');
            var s="";
            var i=2;
            for(var key in rowData){
                s=s+rowName[i++]+':'+rowData[key]+'<br> ';
            }
            alert(s);
        }
    });
    $("#exportExcel").attr('href',baseURL+'ProfessionInfo/export?token='+token);

});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        ProfessionInfo: {},
        professionCode: null
    },
    methods: {
        query: function () {
            $("#exportExcel").attr('href',baseURL+'ProfessionInfo/export?token='+token+'professionName='+vm.q.key);
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.ProfessionInfo = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "ProfessionInfo/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.ProfessionInfo = r.ProfessionInfo;
                vm.professionCode = r.ProfessionInfo.professionCode;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "ProfessionInfo/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.ProfessionInfo.professionCode == vm.professionCode ? "ProfessionInfo/Update" : "ProfessionInfo/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.ProfessionInfo),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'professionName': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.ProfessionInfo.professionCode)){
                alert("专业代码不能为空");
                return true;
            }
            if(isBlank(vm.ProfessionInfo.professionName)){
                alert("专业名称不能为空");
                return true;
            }
            if(isBlank(vm.ProfessionInfo.professionType)){
                alert("专业类型不能为空");
                return true;
            }
            if(isBlank(vm.ProfessionInfo.professionSchoolingYear)){
                alert("学制不能为空");
                return true;
            }
            if(isBlank(vm.ProfessionInfo.professionLevel)){
                alert("层次不能为空");
                return true;
            }
            if(isBlank(vm.ProfessionInfo.professionCollegeCode)){
                alert("所属学院代码不能为空");
                return true;
            }
            if(isBlank(vm.ProfessionInfo.professionCountryCode)){
                alert("国家统编专业代码不能为空");
                return true;
            }
            if(isBlank(vm.ProfessionInfo.professionPriority)){
                alert("优先级不能为空");
                return true;
            }
            if(isBlank(vm.ProfessionInfo.professionCountryName)){
                alert("国家统编专业名称不能为空");
                return true;
            }
            if(isBlank(vm.ProfessionInfo.professionSubjectType)){
                alert("学科类型不能为空");
                return true;
            }
            }
        }
});